package com.fbty.mt.action.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.fbty.base.action.BaseAction;
import com.fbty.base.domain.Page;
import com.fbty.mt.domain.IpInfo;
import com.fbty.mt.domain.OnlyIpInfo;
import com.fbty.mt.domain.TabIpLog;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.IpService;
import com.fbty.mt.service.OtherService;
import com.fbty.mt.service.TabIpLogService;
import com.fbty.mt.utils.EmailUtil;

@SuppressWarnings("serial")
public class IpProxyAction extends BaseAction{
	
	private Page page;
	
	private IpService ipInfoService;
	
	private OtherService otherService;
	
	private TabIpLogService tabIpLogService;
	
	private int pageNo=1;
	
	private int pageSize=10;
	
	private List<String> ipItems;
	
	private boolean flag;
	
	private String type="";
	
	private String country="";
	
	private String region="";
	
	private String city="";
	
	private String item;
	
	private String param;
	
	private List<OnlyIpInfo>  infos;
	
	/**
	 * 批量摄取信息
	 */
	private String message = "";
	/**
	 * 批量提取
	 */
	private Integer num;
	
	@SuppressWarnings("rawtypes")
	private Map parms=new HashMap();
	
	
	@SuppressWarnings("unchecked")
	public String newProxy() throws Exception{
		parms.put("region", "%"+region+"%");
		parms.put("type","".equals(type)?"%"+type+"%":type);
		parms.put("country", "%"+country+"%");
		parms.put("city", "%"+city+"%");
		parms.put("state", 1);
		page = ipInfoService.pageIpInsos(pageNo, pageSize, flag, parms);
		List<IpInfo> list = page.getResult();
		for(IpInfo ip : list){
			int i = ip.getIp().replaceFirst("\\.", "-").indexOf(".");
			ip.setIp(ip.getIp().substring(0,i)+".xxx.xxx");
		}
		return "newProxy";
	}
	
	//独享查询
	@SuppressWarnings("unchecked")
	public String onlyProxy(){
		parms.put("region", "%"+region+"%");
		parms.put("type","".equals(type)?"%"+type+"%":type);
		parms.put("country", "%"+country+"%");
		parms.put("city", "%"+city+"%");
		parms.put("used", 1);
		page = ipInfoService.pageOnlyIpInfos(pageNo, pageSize, flag, parms);
		List<OnlyIpInfo> list = page.getResult();
//		for(OnlyIpInfo ip : list){
//			int i = ip.getExportIp().replaceFirst("\\.", "-").indexOf(".");
//			ip.setExportIp(ip.getExportIp().substring(0,i)+".xxx.xxx");
//		}
		return "onlyProxy";
	}

	public String manyBuy() throws Exception {
		message = "";
		parms.put("region", "%"+region+"%");
		parms.put("type","".equals(type)?"%"+type+"%":type);
		parms.put("country", "%"+country+"%");
		parms.put("city", "%"+city+"%");
		parms.put("state", 1);
		page = ipInfoService.pageIpInsos(pageNo, pageSize, flag, parms);
		List<IpInfo> list = page.getResult();
		for(IpInfo ip : list){
			int i = ip.getIp().replaceFirst("\\.", "-").indexOf(".");
			ip.setIp(ip.getIp().substring(0,i)+".xxx.xxx");
		}
		if(num == 0 || num == null) {
			return SUCCESS;
			
		}
		User u = (User)sessionMap.get("user");
		/**
		 * 根据当前用户所拥有的剩余数量
		 */
		Integer usernum = u.getNum()+u.getMonthnum();
		if(usernum < num ) {
			message="您当前还有<span style='color:green'>"+usernum+"</span>条，您本次购买 "+num+" 条，您还差<span style='color:red'>"+(num-usernum)+"</span>条,请冲值!";
			return SUCCESS;
		}
		/**
		 * 批量提取ip
		 */
		Map<String, Integer> maps = ipInfoService.saveManyBuy(num,u);
		TabIpLog tabIpLog =  new TabIpLog();
		if(maps.get("buy") == 0) {
			message = "<span style='color:red'>sorry,您已经购买过所有的ip!</span>";
		} else if(maps.get("buy") != num){
			message = u.getUsername()+"您好,您本次提取 "+num+"条信息,成功提取<span style='color:green'>"+maps.get("buy")+"</span>条信息,其中有<span style='color:red'>"+(num-maps.get("buy"))+"</span>是重复信息!";
			tabIpLog.setUid(u.getId());
			tabIpLog.setBuyTime(new Date());
			tabIpLog.setBuyNum((maps.get("buy")));
			tabIpLog.setMonthNum(maps.get("monthnum"));
			tabIpLog.setNum(maps.get("num"));
			tabIpLogService.saveLog(tabIpLog);
			EmailUtil.sendEmail("Ip获取消息",message,u.getEmail());
		} else {
			tabIpLog.setUid(u.getId());
			tabIpLog.setBuyTime(new Date());
			tabIpLog.setBuyNum((maps.get("buy")));
			tabIpLog.setMonthNum(maps.get("monthnum"));
			tabIpLog.setNum(maps.get("num"));
			tabIpLogService.saveLog(tabIpLog);
			message = u.getUsername()+"您好,您本次提取 "+num+"条信息,成功提取<span style='color:green'>"+maps.get("buy")+"</span>条信息!";
			EmailUtil.sendEmail("Ip获取消息",message,u.getEmail());
		}
		sessionMap.put("num", maps.get("num"));
		sessionMap.put("monthNum", maps.get("monthnum"));
		u.setNum(maps.get("num"));
		u.setMonthnum(maps.get("monthnum"));
		sessionMap.put("user", u);
		return SUCCESS;
	}
	
	public String getNumber() throws Exception{
		
		String _ipNum = "ipItems";
		HttpSession  session = request.getSession();
		
		List<String> list = (List<String>)(session.getAttribute(_ipNum)==null?new ArrayList<String>():session.getAttribute(_ipNum));
		if(item!=null&&param!=null){
			
			if("add".equals(param)){
				list.add(item);
			}else{
				list.remove(item);
			}
			
			session.setAttribute(_ipNum, list);
		}
		ipItems = list;
		return SUCCESS;
	}
	
	public String buyBatch() throws Exception{
		String _ipNum = "ipItems";
		HttpSession  session = request.getSession();
		List<String> ids = (List<String>)(session.getAttribute(_ipNum)==null?new ArrayList<String>():session.getAttribute(_ipNum));
		
		infos= ipInfoService.buyBatch(ids);
		
		return SUCCESS;
	}
	
	
	public Page getPage() {
		return page;
	}


	public void setPage(Page page) {
		this.page = page;
	}

	public void setIpInfoService(IpService ipInfoService) {
		this.ipInfoService = ipInfoService;
	}


	public int getPageNo() {
		return pageNo;
	}


	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}


	public boolean isFlag() {
		return flag;
	}


	public void setFlag(boolean flag) {
		this.flag = flag;
	}


	@SuppressWarnings("rawtypes")
	public Map getParms() {
		return parms;
	}


	@SuppressWarnings("rawtypes")
	public void setParms(Map parms) {
		this.parms = parms;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setOtherService(OtherService otherService) {
		this.otherService = otherService;
	}



	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getRegion() {
		return region;
	}


	public void setRegion(String region) {
		this.region = region;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public TabIpLogService getTabIpLogService() {
		return tabIpLogService;
	}

	public void setTabIpLogService(TabIpLogService tabIpLogService) {
		this.tabIpLogService = tabIpLogService;
	}
	
	public void setIpItems(List<String> ipItems) {
		this.ipItems = ipItems;
	}
	
	public List<String> getIpItems() {
		return ipItems;
	}
	
	public void setItem(String item) {
		this.item = item;
	}
	
	public String getItem() {
		return item;
	}
	
	public void setParam(String param) {
		this.param = param;
	}
	
	public String getParam() {
		return param;
	}
	
	public void setInfos(List<OnlyIpInfo> infos) {
		this.infos = infos;
	}
	
	public List<OnlyIpInfo> getInfos() {
		return infos;
	}
}
